package com.csu.pet.demo.controller;

import com.csu.pet.demo.domain.Category;
import com.csu.pet.demo.domain.Item;
import com.csu.pet.demo.domain.Product;
import com.csu.pet.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired
    TestService testService;

    //查看商城主页面
    @GetMapping("/viewMain")
    public String viewMain(){
        return "/catalog/Main";
    }

    //查看封面
    @GetMapping("/viewIndex")
    public String viewIndex(){
        return "/index";
    }

    //查看商品大类
    @GetMapping("/viewCategory")
    public String ViewCategory(String categoryId, Model model){

        if(categoryId != null){
            Category category = testService.getCategory(categoryId);
            List<Product> productList = testService.getProductListByCategory(categoryId);
            model.addAttribute("category",category);
            model.addAttribute("productList",productList);
            return "/catalog/Category";

        }
        return "/catalog/Main";
    }

    //查看某一类商品
    @GetMapping("/viewProduct")
    public String ViewProduct(String productId, Model model){
        if(productId != null){
            Product product = testService.getProduct(productId);
            System.out.println("出错啦");
            List<Item> itemList = testService.getItemListByProduct(productId);
            model.addAttribute("product",product);
            model.addAttribute("itemList",itemList);
            return "/catalog/Product";
        }
        return "/catalog/Category";
    }

    //查看具体的商品
    @GetMapping("/viewItem")
    public String ViewItem(String itemId, Model model){
        if(itemId != null){
            //给你id,得到item
            Item item = testService.getItem(itemId);
            Product product = testService.getProduct(item.getProductId());
            model.addAttribute("item",item);
            model.addAttribute("product",product);
            return "/catalog/Item";
        }
        return "/catalog/Product";
    }

    //搜索商品
    @GetMapping("/searchProducts")
    public  String SearchProducts(String keyword,Model model){
        List<Product> productList = testService.searchProductList(keyword);
        model.addAttribute("productList",productList);
        return "/catalog/SearchProducts";
    }

}
